
#include "setting_policy.h"
#include "history_policy.h"
#include "sys_font.h"
#include "pt_fs.h"
static ui_setting_policy_t  ui_set_policy;

int setting_policy_init(void)
{
    ui_set_policy.len = 0;

    ui_set_policy.e_head = (slist_t *)malloc(sizeof(slist_t));
	if (ui_set_policy.e_head == NULL) {
		l_d("e_head null\n");
		return -1;
	}

    slist_init(ui_set_policy.e_head);

    return 0;
}

 ui_link_t * setting_policy_add(int id)
{
    int len = 0;
    ui_link_t * obj = NULL;

    obj = (ui_link_t *)malloc(sizeof(ui_link_t));
    if(obj == NULL){
        l_d("obj err\n");
        return NULL;
    }
    memset(obj, 0, sizeof(ui_link_t));
    slist_init(&(obj->e_tail));
    //**************************************************
    obj->id      = id;
    obj->ui.x    = 0;
    obj->ui.y    = 0;
    obj->ui.bg   = NULL;
    obj->ui.img  = NULL;
    obj->ui.lab  = NULL;
    //**************************************************
    ui_set_policy.len++;
    slist_add_tail(ui_set_policy.e_head,&(obj->e_tail));
    //**************************************************

    return obj;
}

int setting_policy_del(int id)
{
	int ret = 0;
	
	slist_t* node;
	ui_link_t * obj = NULL;

	if(slist_isempty(ui_set_policy.e_head)){
		l_d("list empty.\n");
		return -1;
	}

	slist_for_each(node, (ui_set_policy.e_head))
	{
		obj = slist_entry(node, ui_link_t, e_tail);
		if(obj){
            if(obj->id == id)
		    {
                l_d("del id=%d\n",obj->id);
                ret = 1;
                break;
            }	
		}
	}

	if(ret){
		slist_remove(ui_set_policy.e_head, &(obj->e_tail));
		ui_set_policy.len--;

        if(obj){
            if(obj->ui.bg){
                lv_obj_del(obj->ui.bg);
                obj->ui.bg  = NULL;
                obj->ui.img = NULL;
                obj->ui.lab = NULL;
            }
            free(obj);
            obj = NULL;
        }
	}

	return 0;
}

int setting_policy_get_len(void)
{
    l_d("len=%d\n",ui_set_policy.len);

    return ui_set_policy.len;
}

ui_link_t * setting_policy_get_by_index(int index)
{
    int len = 0;
	slist_t* node;
    ui_link_t * obj = NULL;

	if(slist_isempty(ui_set_policy.e_head)){
		l_d("list empty.\n");
		return NULL;
	}

	slist_for_each(node,(ui_set_policy.e_head))
	{
		obj = slist_entry(node, ui_link_t,e_tail);
		if(obj){
            if(len == index)
            {
                return obj;
            }
		}
        len++;
	}

	return NULL;
}


int setting_policy_destroy(void)
{
	slist_t* node;
    ui_link_t * obj = NULL;

    if(!ui_set_policy.e_head){
        return -1;
    }

    ui_set_policy.len = 0;

	slist_for_each(node, (ui_set_policy.e_head))
	{
		obj = slist_entry(node, ui_link_t, e_tail);
		if(obj)
        {
            slist_remove(ui_set_policy.e_head, &(obj->e_tail));
            free(obj);
            obj = NULL;
		}
	}

    if(ui_set_policy.e_head){
        free(ui_set_policy.e_head);
        ui_set_policy.e_head = NULL;
    }

    return 0;
}

static  char * icon_policy[1] = 
{
    "Return.png",
};

static  char * icon_policy_point[1] = 
{
    "Dot_Default.png",
};

static int setting_policy_clicked_event(lv_event_t* event)
{
    int         id = 0;
    void *      user_data = NULL;
    lv_obj_t *  obj = NULL;

    obj = lv_event_get_target(event);
    if(!obj){
        return-1;
    }
    user_data = (void *)lv_obj_get_user_data(obj);
    id = (int)user_data;
    debug_d("id=%d\n",id);

    // setting_policy_del(id);
    // setting_policy_update_pos();

    history_policy_set_last(id);
    page_pop();

    return 0;
}

static void setting_policy_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("clicked\n");
        setting_policy_clicked_event(event);
    }
}

int setting_policy_info_create(int  id,ui_policy_t * ui,policy_info_t * info)
{
    char txt_buff[256];

    lv_coord_t x = 0;
    lv_coord_t y = 0;
    lv_coord_t w = 0;
    lv_coord_t h = 0;

    ui->bg = lv_obj_create(ui_set_policy.p_list);

    x = ui->x;
    y = ui->y;

    w = 438;
    h = 64;

    debug_d("x:y=%d:%d\n",x,y);

    lv_obj_set_pos(ui->bg,x,y);
    lv_obj_set_size(ui->bg,w,h);

	lv_obj_set_style_radius(ui->bg, 10, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui->bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui->bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui->bg, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui->bg, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui->bg, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui->bg, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui->bg,0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui->bg,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui->bg,LV_OPA_10,0);
    lv_obj_set_style_bg_color(ui->bg,lv_color_make(0xff,0xff,0xff),0);
    #endif     

    lv_obj_set_user_data(ui->bg,(void*)id);

    lv_obj_add_event_cb(ui->bg, setting_policy_event_callback, LV_EVENT_ALL, NULL);
    //********************************************************************
    ui->img = lv_img_create(ui->bg);
    lv_img_set_src(ui->img,pt_fs_get(icon_policy_point[0]));
    lv_obj_set_pos(ui->img,17,30);
    //********************************************************************
    ui->lab = lv_label_create(ui->bg);
    lv_obj_set_pos(ui->lab,38,12);
    lv_obj_set_size(ui->lab,286,40);
    lv_obj_set_style_text_align(ui->lab,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui->lab,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui->lab,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui->lab, lv_font_get(24), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,256,"%d.%d.%d",info->t_year,info->t_month,info->t_day);
    lv_label_set_text(ui->lab,txt_buff);
    //********************************************************************


    return 0;
}

int setting_policy_update_pos(void)
{
    int i   = 0;
    int len = 0;

    ui_link_t *  obj = NULL;

    len = setting_policy_get_len();
    for(i=0;i<len;i++)
    {
        obj = setting_policy_get_by_index(i);
        if(!obj){
             debug_d("get err\n");
            continue;
        }

        obj->ui.x = 16;
        obj->ui.y = i*72;

        lv_obj_set_pos(obj->ui.bg,obj->ui.x,obj->ui.y);
    }

    return 0;
}


static int setting_policy_esc_clicked(lv_event_t* event)
{
    page_pop();

    return 0;
}

static void setting_policy_esc_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("esc\n");
        setting_policy_esc_clicked(event);
    }
}

int ui_setting_policy_create(lv_obj_t *superview)
{
    int i   = 0;
    int len = 0;

    lv_coord_t x  = 0;
    lv_coord_t y  = 0;
    lv_coord_t w  = 0;
    lv_coord_t h  = 0;
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    char txt_buff[256];

    ui_link_t*         ui_link = NULL;
    history_policy_t * info    = NULL;
    //**********************************************************************************************
    debug_d("\n");
    setting_policy_init();
    //**********************************************************************************************

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    debug_d("w:h=%d:%d\n",pw,ph);

    ui_set_policy.img_bg = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_policy.img_bg,16,8);
    lv_obj_set_size(ui_set_policy.img_bg,32,32);

	lv_obj_set_style_radius(ui_set_policy.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_policy.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_policy.img_bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_policy.img_bg, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_policy.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_policy.img_bg, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_policy.img_bg, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_policy.img_bg, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_set_policy.img_bg,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_policy.img_bg,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_policy.img_bg,lv_color_make(0xff,0,0),0);
    #endif 
    //************************
    ui_set_policy.img_esc = lv_img_create(ui_set_policy.img_bg);
    lv_img_set_src(ui_set_policy.img_esc,pt_fs_get(icon_policy[0]));
    lv_obj_set_pos(ui_set_policy.img_esc,0,0);

    lv_obj_add_event_cb(ui_set_policy.img_bg,setting_policy_esc_event_callback,LV_EVENT_ALL,NULL);
    //**********************************************************************************************
    ui_set_policy.title = lv_label_create(superview);
    lv_obj_set_pos(ui_set_policy.title,102,6);
    lv_obj_set_size(ui_set_policy.title,276,25);
    lv_obj_set_style_text_align(ui_set_policy.title,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_policy.title,LV_OPA_80,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_policy.title,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_policy.title, lv_font_get(20), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_policy.title, "历史隐私条款");
    //**********************************************************************************************

    ui_set_policy.p_list = lv_obj_create(superview);

    x = 0;
    y = 60;
    w = pw;
    h = ph - 60;

    lv_obj_set_pos(ui_set_policy.p_list,x,y);
    lv_obj_set_size(ui_set_policy.p_list,w,h);

	lv_obj_set_style_radius(ui_set_policy.p_list, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_policy.p_list, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_policy.p_list, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_policy.p_list, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_policy.p_list, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_policy.p_list, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_policy.p_list, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_policy.p_list, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_set_policy.p_list,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_policy.p_list,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_policy.p_list,lv_color_make(0xff,0,0),0);
    #endif 
    //**********************************************************************
    len = history_policy_get_len();
    for(i=0;i<len;i++)
    {
        ui_link =  setting_policy_add(i);
        if(!ui_link){
            debug_d("add err\n");
            continue;
        }

        info = history_policy_get_by_index(i);
        if(!info){
            debug_d("get err\n");
            continue;
        }

        ui_link->ui.x = 16;
        ui_link->ui.y = i*72;

        setting_policy_info_create(ui_link->id,&ui_link->ui,&info->e_attr);
    }
    //**********************************************************************


    return 0;
}


int ui_setting_policy_destroy(lv_obj_t *superview)
{
    if(ui_set_policy.img_bg){
        lv_obj_del(ui_set_policy.img_bg);
        ui_set_policy.img_bg = NULL;
        ui_set_policy.img_esc = NULL;
    }
    if(ui_set_policy.title){
        lv_obj_del(ui_set_policy.title);
        ui_set_policy.title = NULL;
        ui_set_policy.img_esc = NULL;
    }
    if(ui_set_policy.p_list){
        lv_obj_del(ui_set_policy.p_list);
        ui_set_policy.p_list = NULL;
    }

    setting_policy_destroy();

    return 0;
}


static void setting_policy_on_view_did_load(page_base_t* self)
{
    ui_setting_policy_create(self->root);
}

static void setting_policy_on_view_did_unload(page_base_t* self)
{
    ui_setting_policy_destroy(self->root);
}

static page_vtable_t setting_policy_param = {
    .on_custom_attr_config  = NULL,
    .on_view_load           = NULL,
    .on_view_did_load       = setting_policy_on_view_did_load,
    .on_view_will_appear    = NULL,
    .on_view_did_appear     = NULL,
    .on_view_will_disappear = NULL,
    .on_view_did_disappear  = NULL,
    .on_view_did_unload     = setting_policy_on_view_did_unload,
};


page_vtable_t* lv_page_setting_policy_create(void)
{
    return &setting_policy_param;
}
